/*
 * Created by kopper on 01.02.15 08:59
 * (C) Copyright 2015 kopperek@gmail.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

package app.android.kopper.gpxdownloader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity {

    private DataReaderAsyncTask dataReceiver=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setProgressVisible(true);
                dataReceiver=new DataReaderAsyncTask();
                dataReceiver.connectActivity(MainActivity.this);
                dataReceiver.execute((UsbManager)getSystemService(Context.USB_SERVICE));
            }
        });

        dataReceiver=(DataReaderAsyncTask)getLastNonConfigurationInstance();
        if(dataReceiver!=null) {
            dataReceiver.connectActivity(this);
            setProgressVisible(true);
        }
    }

    private void setProgressVisible(boolean visible) {
        findViewById(R.id.button).setEnabled(!visible);
        findViewById(R.id.progressBar).setVisibility(visible?View.VISIBLE:View.INVISIBLE);
        findViewById(R.id.subTaskName).setVisibility(visible?View.VISIBLE:View.INVISIBLE);
        findViewById(R.id.taskName).setVisibility(visible?View.VISIBLE:View.INVISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(dataReceiver!=null) {
            dataReceiver.disconnectActivity();
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return dataReceiver;
    }

    public void updateProgress(ProgressParam value) {
        ProgressBar progressBar=(ProgressBar)findViewById(R.id.progressBar);
        progressBar.setMax(value.getMax());
        progressBar.setProgress(value.getProgress());
        ((TextView)findViewById(R.id.subTaskName)).setText(value.getSubTaskName());
        ((TextView)findViewById(R.id.taskName)).setText(value.getTaskName());
    }

    public void done(final DataReaderResult result) {
        dataReceiver=null;
        setProgressVisible(false);
        if(result!=null) {
            AlertDialog ad = new AlertDialog.Builder(this).create();
            ad.setCancelable(false); // This blocks the 'BACK' button
            if(result.getE()!=null)
                ad.setMessage(result.getE().getLocalizedMessage());
            else
                ad.setMessage(result.getMessage());
            ad.setButton(getString(R.string.button_ok),new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                    dialog.dismiss();
                    finish(); //done
                }
            });
            ad.show();
        }
    }


}
